# -*- coding: utf-8 -*-
#Player class

import pyglet
from pyglet.gl import *
import math

import bullets
from constants import *
from objects import *

class Player(GameObject):
    def __init__(self, image, x, y, batch=None):
        try:
            image.anchor_x = image.width/2
            image.anchor_y = image.height/2
        except AttributeError:
            for frame in image.frames:
                frame.image.anchor_x = frame.image.width/2
                frame.image.anchor_y = frame.image.height/2
        self.sprite = pyglet.sprite.Sprite(image, x=x, y=y, batch=batch)
        
        self.left = False
        self.right = False
        self.up = False
        self.down = False
        self.shooting = False
        self.is_dead = False

        self.vel = 0
        #self.velx = 0
        #self.vely = 0
        self.cooldown_counter = 0
        super(Player, self).__init__()

    #block of movement functions
    def start_movin_up(self):
        self.up = True
    def start_movin_down(self):
        self.down = True
    def start_movin_left(self):
        self.left = True
    def start_movin_right(self):
        self.right = True
    def start_shootin(self):
        self.shooting = True
    def stop_movin_up(self):
        self.up = False
    def stop_movin_down(self):
        self.down = False
    def stop_movin_left(self):
        self.left = False
    def stop_movin_right(self):
        self.right = False
    def stop_shootin(self):
        self.shooting = False

    def be_hit(self, damage):
        self.life -= damage
        if self.life <= 0:
            self.die()

    def die(self):
        print "Nu dog han!"
        self.is_dead = True

    def update(self, dt):
        #move
        self._update_position(dt)
        #fire missailis
        self._do_shoot(dt)
        #do collision stuff
        self._check_collisions()
        self._check_if_hit()

    def _update_position(self, dt):
        self.vel = friction * self.vel
        #self.vely = friction * self.vely
        
        acc = 0
        if self.up:
            acc = self.accel
        if self.down:
            acc = -self.accel
        if self.right:
            self.sprite.rotation += self.rot_speed
        if self.left:
            self.sprite.rotation -= self.rot_speed
        self.vel += acc * dt
        #self.vely += acc * dt * math.cos(math.radians(self.sprite.rotation))
       
        #vel = math.hypot(self.vely, self.velx)
        #ang = 90 - math.degrees(math.atan2(self.vely, self.velx))
        #vel_ang = (ang%360) + \
           #int(((self.sprite.rotation%360) - (ang%360)) / 2)
        #self.velx = vel * math.sin(math.radians(vel_ang))
        #self.vely = vel * math.cos(math.radians(vel_ang))

        self.sprite.x += int(self.vel * dt * math.sin(math.radians(self.sprite.rotation)))
        self.sprite.y += int(self.vel * dt * math.cos(math.radians(self.sprite.rotation)))

    def _check_collisions(self):
        lastrect = self.rect
        self.rect = self.get_rect()
        for building in World.buildings:
            if collision(self.rect, building.rect):
                self.keep_out_of(building.rect, lastrect)
                if lastrect[1] <= building.rect[0] or lastrect[0] >= \
                    building.rect[1]:
                    #hit from the side
                    if abs(self.vel) > hit_threshold:
                        building.be_hit(abs(self.vel))
                    self.vel = -bounceback * self.vel
                if lastrect[3] <= building.rect[2] or lastrect[2] >= \
                      building.rect[3]:
                    #vertical hit
                    if abs(self.vel) > hit_threshold:
                        building.be_hit(abs(self.vel))
                    self.vel = -bounceback * self.vel

    def _check_if_hit(self):
        for bullet in World.enemy_bullets:
            if collision(self.rect, bullet.rect):
                self.be_hit(bullet.damage)
                bullet.die()

    def _do_shoot(self, dt):
        if self.shooting and self.cooldown_counter <= 0:
            self._shoot()
            self.cooldown_counter = self.cooldown
        if self.cooldown_counter > 0:
            self.cooldown_counter -= dt

    def _shoot(self):
        b  = bullets.Missile(self.sprite.x, self.sprite.y, 
           self.sprite.rotation, self.missile_dmg, self.missile_vel)
        World.player_bullets.append(b)


    def draw_life(self, x, y, width):
        w = width/self.max_life * self.life
        verts = [x,y+10, #topleft
                x,y, #bottomleft
                x+w,y,#bottomright
                x+w,y+10,] #topright
        color = [1.0, 0.0, 0.0, 
                1.0, 0.0, 0.0, 
                0.0, 1.0, 0.0, 
                0.0, 1.0, 0.0,]
        pyglet.graphics.draw(4, GL_QUADS,
            ('v2i', verts), ('c3f',color) )

class Bengt(Player):
    def __init__(self, x, y):
        img = pyglet.resource.image("tanksprite.png")
        img.anchor_x = img.width/2
        img.anchor_y = img.height/2
        self.max_life = self.life = 200
        self.rot_speed = 5
        self.accel = 800
        self.missile_dmg = 100
        self.missile_vel = 330
        self.cooldown = .5
        Player.__init__(self, img, x, y)

